package com.base.api.service.broadcast.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.api.dto.broadcast.BroadcastListDTO;
import com.base.api.service.broadcast.BroadcastApiService;
import com.base.api.service.broadcast.BroadcastBk;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.entity.Broadcast;
import com.base.self.mapper.BroadcastMapper;
import com.base.utils.Redis.RedisUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

@Service
public class BroadcastApiServiceImpl implements BroadcastApiService {

    private final BroadcastMapper broadcastMapper;

    private final RedisUtil redisUtil;

    private final static String BK = "B_K";

    private final static int times = 1;

    public BroadcastApiServiceImpl(BroadcastMapper broadcastMapper, RedisUtil redisUtil) {
        this.broadcastMapper = broadcastMapper;
        this.redisUtil = redisUtil;
    }

    @Override
    @Transactional
    public ResponseResult broadcastList(BroadcastListDTO broadcastListDTO) {
        String bk = redisUtil.get(BK);
        if (StringUtils.isEmpty(bk)) {
            return ResponseResultUtils.getResponseResultS("查询成功", setData(broadcastListDTO));
        }
        final AtomicReference<BroadcastBk> atomicReference = new AtomicReference<>();
        try {
            atomicReference.set(JSONObject.parseObject(bk, BroadcastBk.class));
            if (atomicReference.get() != null) {
                Date offset = DateUtil.offsetMinute(DateUtil.date(atomicReference.get().getCreateTime()), times);
                if (DateUtil.date().isAfter(offset)) {/*过期删除*/
                    redisUtil.del(BK);
                    //List<Broadcast> list = (List<Broadcast>) atomicReference.get().getData().get("rows");
                    //list.forEach(broadcastMapper::updateById);
                    return ResponseResultUtils.getResponseResultS("查询成功", setData(broadcastListDTO));
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            atomicReference.set(new BroadcastBk(setData(broadcastListDTO), System.currentTimeMillis()));
        }
        return ResponseResultUtils.getResponseResultS("查询成功", atomicReference.get().getData());
    }

    private Map<String, Object> setData(BroadcastListDTO broadcastListDTO){
        final Map<String, Object> data = PageUtils.getDateMap(() ->
                broadcastMapper.selectPage(
                        PageUtils.getPage(
                                new Page<>(),
                                broadcastListDTO.getOffset(),
                                broadcastListDTO.getLimit()
                        ),
                        new LambdaQueryWrapper<Broadcast>()
                                .eq(Broadcast::getStatus, 0)
                                .orderByAsc(Broadcast::getCreateTime)
                )
        );
        redisUtil.set(BK, new BroadcastBk(data, System.currentTimeMillis()));
        return data;
    }
}
